package cn.itsource.hbc.quartz.job;

import cn.itsource.hbc.order.domain.OrderAdopt;
import cn.itsource.hbc.order.service.IOrderAdaptService;
import cn.itsource.hbc.pay.contants.BusinessContants;
import cn.itsource.hbc.pay.domain.PayBill;
import cn.itsource.hbc.pay.service.IPayBillService;
import cn.itsource.hbc.pet.domain.Pet;
import cn.itsource.hbc.pet.service.IPetService;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;
@Component
public class MainJob implements Job{

    @Autowired
    private IPayBillService payBillService;
    @Autowired
    private IOrderAdaptService orderAdaptService;
    @Autowired
    private IPetService petService;


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        // 通过任务详情拿到数据map
        JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        // 获取map中参数params
        Map<String,Object> params = (Map<String, Object>) jobDataMap.get("params");
        // 从params中拿到orderSn
        String orderSn = (String) params.get("orderSn");
        // 通过orderSn拿到支付单对象
        PayBill payBill = payBillService.queryByOrderSn(orderSn);
        // 把订单状态设置为-1
        payBill.setState(-1);
        // 设置下架时间
        payBill.setUpdateTime(new Date());
        payBillService.update(payBill);

        switch (payBill.getBusinessType()) {
            case BusinessContants.ORDER_ADOPT:
                OrderAdopt orderAdopt = orderAdaptService.queryByOrderSn(orderSn);
                orderAdopt.setState(-1);
                orderAdaptService.update(orderAdopt);
                // 拿到宠物对象进行重新上架
                Pet pet = petService.loadById(orderAdopt.getPet_id());
                pet.setState(1);
                pet.setOnsaletime(new Date());
                petService.update(pet);
                break;
            case BusinessContants.ORDER_PRODUCT:
                break;
            case BusinessContants.ORDER_GOODS:
                break;

        }

    }
}
